package com.woniuxy;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
public class StudentTypeController {

    @Autowired
    StudentTypeService studentTypeService;

    @RequestMapping("/")
    public String root() {
        return "types";
    }

//    @RequestMapping("all")
//    public List<Studenttype> all() {
//        List<Studenttype> types = studentTypeService.getAll();
//        return types;
//    }

    @RequestMapping("/add")
    public String add() {
        return "add";
    }

    @RequestMapping("doAdd")
    public String doAdd(String name) {
        Studenttype studenttype = new Studenttype();
        studenttype.setName(name);


        studentTypeService.add(studenttype);

        return "redirect:/";
    }


    @RequestMapping("/update")
    public String update(Integer typeId, Model model) {
        Studenttype type = studentTypeService.getById(typeId);
        model.addAttribute("type", type);

        return "update";
    }

    @RequestMapping("doUpdate")
    public String doAdd(Integer id, String name) {
        Studenttype booktype = studentTypeService.getById(id);
        booktype.setName(name);

        studentTypeService.updateById(booktype);

        return "redirect:/";
    }

    @RequestMapping("delete")
    public String doAdd(Integer typeId) {
        studentTypeService.deleteById(typeId);

        return "redirect:/";
    }


    @RequestMapping("/pageByType")
    @ResponseBody
    public Page<Studenttype> pageByType(Integer currentPage, Integer pageSize) {
        return studentTypeService.pageByType(currentPage, pageSize);

    }


}
